/**
 * 
 */
package com.sidom.movieIndexer.mock;

import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.BasicResponseHandler;
import org.mockito.Mockito;

import com.sidom.movieIndexer.ConstantsForTesting;
import com.sidom.movieIndexer.util.TestUtil;
import static org.mockito.Mockito.*;

/**
 * Mocks the call to imdb.
 * Returns a html file on the classpath.
 * To create a new mock html file.
 * @author Siebe
 */
public class ImdbMock {
	public enum HtmlFile{
		HTML_EQUILIBRIUM("equilibrium.html"),
		HTML_SWORDFISH("swordFish.html"),
		HTML_GONE_IN_60_SECONDS("goneIn60Seconds.html"),
		HTML_YOUTUBE("youtube.html"),
		HTML_BOX_OFFICE("boxOffice.html"),
		HTML_BOURNE_IDENTITY("bourneIdentity.html"),
		HTML_THE_ISLAND("theIsland.html"),
		HTML_TRANSPORTER("transporter.html"),
		HTML_MATRIX("matrix.html"),
		HTML_THE_ITALIAN_JOB_CORRECT("theItalianJobCorrect.html");
		
		private final String htmlFile;
		
		/**
		 * Constructor
		 * @param html The html file.
		 */
		private HtmlFile(String htmlFile){
			this.htmlFile = htmlFile;
		}

		/**
		 * @return the htmlFile
		 */
		public String getHtmlFile() {
			return htmlFile;
		}
	}
	
	/**
	 * Create a mock for the html of a return url.
	 * @param imdbUrl The imdb url to mock.
	 * @param httpClient The HttpClient that is used to make the call.
	 * @param htmlFile The html file.
	 */
	public static void createMockForImdbUrl(String imdbUrl, HttpClient httpClient, HtmlFile htmlFile) {
		ClientConnectionManager mockedClientConnectionManager = mock(ClientConnectionManager.class);
		try {
			when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(BasicResponseHandler.class))).thenReturn(TestUtil.getFileFromClassPath(htmlFile.getHtmlFile(), ConstantsForTesting.TEST_RESOURCE_DIRECORY_HTML));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			when(httpClient.getConnectionManager()).thenReturn(mockedClientConnectionManager);
		}
	}
//	
//	/**
//	 * Create a mock response for all the movies with the avi extension.
//	 * @param restTemplate The {@link RestTemplate}
//	 */
//	public static void createMockForAviMovies(RestTemplate restTemplate) {
//		Map<String, String> moviesAndJson = new HashMap<String, String>();
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.BOURNE_IDENTITY_CD1).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_BOURNE_IDENTITY));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_MATRIX).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_MATRIX));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_ISLAND).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_THE_ISLAND));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.SWORDFISH_CD1).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_SWORDFISH));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.EQUILIBRIUM_CD1).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_EQUILIBRIUM));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.YOUTUBE).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_YOUTUBE));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.BOX_OFFICE).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_BOX_OFFICE));
//		createMockForMovies(moviesAndJson, restTemplate);
//	}
//	
//	/**
//	 * Create a mock response for all the movies with the mp4 extension.
//	 * @param restTemplate The {@link RestTemplate}
//	 */
//	public static void createMockForMp4Movies(RestTemplate restTemplate) {
//		Map<String, String> moviesAndJson = new HashMap<String, String>();
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.GONE_IN_60_SECONDS).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_GONE_IN_60_SECONDS));
//		moviesAndJson.put(MovieFactory.createMovie(TestMovie.TRANSPORTER).getMovieFile().getCleanedFileName(), TestUtil.getJsonFromClassPath(ImdbMock.HTML_TRANSPORTER));
//		createMockForMovies(moviesAndJson, restTemplate);
//	}
}
